package com.edu.whty.controller;

import com.alibaba.fastjson.JSON;
import com.edu.whty.common.constants.CodeConstants;
import com.edu.whty.common.constants.Constant;
import com.edu.whty.common.tools.*;
import com.edu.whty.common.tools.redis.RedisUtil;
import com.edu.whty.entity.base.AttachFile;
import com.edu.whty.entity.base.CloudPage;
import com.edu.whty.entity.base.JobMonitor;
import com.edu.whty.entity.base.ResponseEntity;
import com.edu.whty.entity.cloud.*;
import com.edu.whty.entity.school.VisualRangePrivDto;
import com.edu.whty.entity.share.ResShare;
import com.edu.whty.service.AttachFileService;
import com.edu.whty.service.CourseService;
import com.edu.whty.service.JobMonitorService;
import com.edu.whty.service.ResShareService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/cloud")
public class ShangHaiCloudController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private CourseService courseService;



    @RequestMapping(value = "/saveShangHaiCloudRes", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity saveXitiCloudRes(HttpServletRequest request,@RequestBody List<UserResRequest2> userResList) {
        try {
            if (userResList != null && userResList.size() > 0) {
                Map<String, Object> map = courseService.saveShangHaiCloudRes(userResList);
                return ResponseEntity.getSuccessResponse(map);
            }else {
                return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "添加资源发生异常");
            }
        } catch (Exception e) {
            logger.error("添加资源发生异常" + e.getMessage(), e);
            return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "添加资源发生异常");

        }
    }


    @RequestMapping(value = "/deleteShangHaiCloud", method = RequestMethod.POST)
    public ResponseEntity deleteCloud(HttpServletRequest request, HttpServletResponse response,
                                      @RequestBody List<String> resIdList) {
        try {

            if (resIdList == null || resIdList.size() == 0) {
                return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "参数不能为空");
            }

            courseService.deleteShangHaiCloud(resIdList);
            return ResponseEntity.getSuccessResponse();
        } catch (Exception e) {
            logger.error("删除资源发生异常" + e.getMessage());
            return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "删除资源发生异常");
        }
    }



    @RequestMapping(value = "/updateShangHaiCloud", method = RequestMethod.POST)
    public ResponseEntity updateCloud(HttpServletRequest request,@RequestBody TbCourseRes param) {
        try {

            if (param == null) {
                return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "参数不能为空");
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            TbCourseUser tbCourseUser = courseService.getUserByResId(param.getShangHaiResId());
            TbCourseRes tbCourseRes = courseService.getResByResId(param.getShangHaiResId());

            tbCourseUser.setFid(param.getFid());
            tbCourseUser.setTitle(param.getTitle());
            tbCourseUser.setIsShare(param.getIsShare());
            tbCourseUser.setCourseModifyTime(sdf.format(new Date()));
            tbCourseRes.setPreviewUrl(param.getPreviewUrl());
            tbCourseRes.setDownUrl(param.getDownUrl());
            tbCourseRes.setFid(param.getFid());
            tbCourseRes.setSrcFid(param.getFid());
            tbCourseRes.setTitle(param.getTitle());
            tbCourseRes.setModifyTime(sdf.format(new Date()));
            courseService.updateCloud(tbCourseRes);
            courseService.updateBycourseResId(tbCourseUser);

            courseService.updateCloud(tbCourseRes);
            courseService.updateBycourseResId(tbCourseUser);
            return ResponseEntity.getSuccessResponse();
        } catch (Exception e) {
            logger.error("修改资源发生异常" + e.getMessage());
            return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "修改资源发生异常");
        }
    }




}
